//
//  UIButton+ImgWithText.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/3.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "UIButton+ImgWithText.h"
#import <UIButton+WebCache.h>
#import "UIImage+ColorImage.h"

@implementation UIButton (ImgWithText)

/*
 * 设置button的title和image的布局样式、间距
 */

- (void)buttonWithTitle:(NSString *)title titleFont:(UIFont *)font titleColor:(UIColor *)color imageType:(ImageType)imgType imageName:(NSString *)img withTarget:(id)target action:(SEL)action style:(ImgTextBtnStyle)style margin:(CGFloat)margin{
    
    if (imgType == ImageTypeImg) {
        [self setImage:[UIImage imageNamed:img] forState:UIControlStateNormal];
    }else if (imgType == ImageTypeUrl){
        [self sd_setBackgroundImageWithURL:[NSURL URLWithString:img] forState:UIControlStateNormal placeholderImage:[UIImage imageWithColor:XJCOLOR_RGB(221, 221, 221)]];
    }
    [self setTitle:title forState:UIControlStateNormal];
    [self setTitleColor:color forState:UIControlStateNormal];
    self.titleLabel.font=font;
    [self addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    // 1. 得到imageView和titleLabel的宽、高
    CGFloat imageWith = self.imageView.frame.size.width;
    CGFloat imageHeight = self.imageView.frame.size.height;
    
    CGFloat labelWidth = 0.0;
    CGFloat labelHeight = 0.0;
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
        // 由于iOS8中titleLabel的size为0，用下面的这种设置
        labelWidth = self.titleLabel.intrinsicContentSize.width;
        labelHeight = self.titleLabel.intrinsicContentSize.height;
    } else {
        labelWidth = self.titleLabel.frame.size.width;
        labelHeight = self.titleLabel.frame.size.height;
    }
    // 2. 声明全局的imageEdgeInsets和labelEdgeInsets
    UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
    UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
    // 3. 根据style和space得到imageEdgeInsets和labelEdgeInsets的值
    switch (style) {
        case ImgTextBtnStyleTop:
        {
            imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-margin/2.0, 0, 0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-margin/2.0, 0);
        }
            break;
        case ImgTextBtnStyleLeft:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, -margin/2.0, 0, margin/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, margin/2.0, 0, -margin/2.0);
        }
            break;
        case ImgTextBtnStyleBottom:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-margin/2.0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-margin/2.0, -imageWith, 0, 0);
        }
            break;
        case ImgTextBtnStyleRight:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+margin/2.0, 0, -labelWidth-margin/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith-margin/2.0, 0, imageWith+margin/2.0);
        }
            break;
        default:
            break;
    }
    
    // 4. 赋值
    self.titleEdgeInsets = labelEdgeInsets;
    self.imageEdgeInsets = imageEdgeInsets;
    
}

/*
 * 设置button样式、间距
 */
- (void)buttonWithImgTextBtnStyle:(ImgTextBtnStyle)style margin:(CGFloat)margin{
    // 1. 得到imageView和titleLabel的宽、高
    CGFloat imageWith = self.imageView.frame.size.width;
    CGFloat imageHeight = self.imageView.frame.size.height;
    
    CGFloat labelWidth = 0.0;
    CGFloat labelHeight = 0.0;
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
        // 由于iOS8中titleLabel的size为0，用下面的这种设置
        labelWidth = self.titleLabel.intrinsicContentSize.width;
        labelHeight = self.titleLabel.intrinsicContentSize.height;
    } else {
        labelWidth = self.titleLabel.frame.size.width;
        labelHeight = self.titleLabel.frame.size.height;
    }
    // 2. 声明全局的imageEdgeInsets和labelEdgeInsets
    UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
    UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
    // 3. 根据style和space得到imageEdgeInsets和labelEdgeInsets的值
    switch (style) {
        case ImgTextBtnStyleTop:
        {
            imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-margin/2.0, 0, 0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-margin/2.0, 0);
        }
            break;
        case ImgTextBtnStyleLeft:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, -margin/2.0, 0, margin/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, margin/2.0, 0, -margin/2.0);
        }
            break;
        case ImgTextBtnStyleBottom:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-margin/2.0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-margin/2.0, -imageWith, 0, 0);
        }
            break;
        case ImgTextBtnStyleRight:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+margin/2.0, 0, -labelWidth-margin/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith-margin/2.0, 0, imageWith+margin/2.0);
        }
            break;
        default:
            break;
    }
    
    // 4. 赋值
    self.titleEdgeInsets = labelEdgeInsets;
    self.imageEdgeInsets = imageEdgeInsets;
}


@end
